import * as fs from 'fs';
import {Request} from 'express';
import {config} from "../config/config";

export const getClientIp = (req: Request): string => {
    const forwarded = req.headers['x-forwarded-for'] as string | undefined;
    if (forwarded) return forwarded.split(',')[0].trim();
    return req.socket.remoteAddress || 'unknown';
}


// 读取 JSON 文件
export const getJson = (filePath: string = config.json_file): Promise<string[]> => {
    return new Promise((resolve) => {
        const data = fs.readFileSync(filePath, 'utf8');
        resolve(JSON.parse(data).excludeAppids);
    });
};

// 写入 JSON 文件
export const setJson = (data: any, filePath: string = config.json_file) => {
    const jsonData = JSON.stringify({excludeAppids: data}, null, 2); // 格式化 JSON 数据
    fs.writeFileSync(filePath, jsonData, 'utf8');
    console.log('Data successfully written to the file.');
};

export const getExcludes = () => {

}